define(function(require, exports, module) {
    "use strict";
    var $ = require("$"),
        app = require("dist/application/app"),
        config = app.config,
        method = app.method;

    exports.init = function(form) { 
        var $js_blockitems = $(".js_blockitems");
        $js_blockitems.length && (function() {
            require.async(["kindeditor", "assets/kindeditor/themes/default/default.css"], function() {
                var editor = KindEditor.editor({
                    allowFileManager: true
                });
                $(form).on("click", ".thumb-select", function(e) {
                    var $this = $(this),
                        $hide_input = $this.find("input[type=text]"),
                        $img = $this.find("img");

                    var imagePath = $hide_input.val();
                    editor.loadPlugin('image', function() {
                        editor.plugin.imageDialog({
                            imageUrl: imagePath,
                            clickFn: function(url, title, width, height, border, align) {
                                $hide_input.val(url).hide();
                                if ($img.length > 0) {
                                    $img.attr('src', url).removeClass("hide")
                                }
                                $this.addClass('selected');
                                $img.removeClass('thumb-default-img')
                                editor.hideDialog();
                            }
                        });
                    });
                })
            });
            $(form).on("click", ".js_blockitems_add", function() {
                var $blockitems = $(".js_blockitems"),
                    count = $blockitems.find('.blockitem').length;
                if (count < 10) {
                    $(".js_blockitems",form).append(template("blockitem_tmp",{index:count}))
                } else {
                    config.msg.info(config.lang.maxItem10)
                }

            });
            $(form).on("change", ".js_nav_type", function() {
                var selected = $(this).find('option:selected');
                var type = selected.data("type");
                var flag = (type == "inner");
                var blockitem = $(this).closest('.blockitem');
                blockitem.find("input.error").removeClass('error');
                blockitem.find("span.error").remove();
                $(".js_nav_page", blockitem).toggle(flag);
                $(".js_nav_text", blockitem).toggle(!flag);
            });
            $(form).on("click", '.js_del_group', function(e) {
                var $this = $(this);
                var $group = $this.closest('.blockitem');
                if ($group.siblings().length > 0) {
                    var confirm = $group.find(".selected").length > 0;

                    var done = function() {
                        $group.fadeOut(function() {
                            $group.remove();
                        });
                    }
                    confirm && config.msg.msgbox.confirm(config.lang.confirmRemove, function(result) {
                        if (result) {
                            done();
                        }
                    });
                    !confirm && done();
                } else {
                    config.msg.info(config.lang.minOneItem)
                }
            });
        })();


        var $js_item_list = $(".js_item_list",form);
        $js_item_list.length && (function() {
            var $js_make_item = $(".js_make_item",form);
            var $js_add_item = $(".js_add_item",form);
            
            $js_add_item.on("click", function() {
                if ($(".dd-item",form).length > 3) { 
                    config.msg.info(config.lang.maxFnLength)
                } else { 
                    $(".js_item_modal",form).modal("show");
                }

            });

            var done = function() {
                $js_item_list.nestable({
                    maxDepth: 1
                });
                $('.dd-handle a',form).on('mousedown', function(e) {
                    e.stopPropagation();
                });
            }
            require.async("nestable", done);
            $js_make_item.on("click", function() {
                    var $item = $('.js_item_modal input[name="item"]:checked',form);
                    var add_item_length = $(".dd-item",form).length;
                    var data = {
                        name: $item.data("name"),
                        val: $item.val(),
                        index: add_item_length,
                        id: add_item_length
                    };
                    $("ol", $js_item_list).append(template.compile(require('./template/item.html'))(data));
                    $(".js_item_modal").modal("hide");
                    done() 
            });
        })();
        $(form).on("change", ".js_link_type", function() {
            var selected = $(this).find('option:selected');
            var type = selected.data("type");
            var flag = (type == "inner");
            var not = (type == "not");
            var row = $(this).closest('.row');
            row.find("input.error").removeClass('error');
            row.find("span.error").remove();
            not && $(".js_link_page", row).add(".js_link_text", row).hide()
            if (!not) {
                $(".js_link_page", row).toggle(flag);
                $(".js_link_text", row).toggle(!flag);
            };
        });

        var $select2 = $('[data-toggle="select2"]',form);
		$select2.length > 0 && require.async(['select2','style/plugins/select2/select2.min.css'], function() {
			$select2.each(function() {
				var t = $(this),
					n = {}, c = t.data('maxcount'),
					o = t.data('chang'),
					r = t.data('remote') || t.attr('href');
				c && (n.maximumSelectionSize = c);
				if (o) {
					switch (o) {
						case "submit":
							{
								t.on("change", function(e) {
									t.closest("form ").submit();
								})
							}
					}
				};
				t.select2(n)
			})
		});
		var $js_auto_select=$(".js_auto_select",form);
		$js_auto_select.length && (function() {
			var $style=$(".js_auto_select option:selected",form),type=$style.data("type"),
			$js_auto_classes=$(".js_auto_classes",form),$js_auto_actity=$(".js_auto_actity",form);
			if(type=="classes"){
				$js_auto_classes.removeClass('hide');
				$js_auto_actity.addClass('hide');
			}else if(type=="actity"){
				$js_auto_classes.addClass('hide');
				$js_auto_actity.removeClass('hide');
			}else{
				$js_auto_classes.addClass('hide');
				$js_auto_actity.addClass('hide');
			}
			$js_auto_select.change(function(){
				var type=$(this).find("option:selected").data("type");
				if(type=="classes"){
					$js_auto_classes.removeClass('hide');
					$js_auto_actity.addClass('hide');
				}else if(type=="actity"){
					$js_auto_classes.addClass('hide');
					$js_auto_actity.removeClass('hide');
				}else{
					$js_auto_classes.addClass('hide');
					$js_auto_actity.addClass('hide');
				}
			});
		})();
    }
	
})
